package com.czl.module_service.viewmodel

import android.view.View
import androidx.databinding.ObservableField
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.*
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper
import com.czl.module_service.R
import com.czl.module_service.view.ListSheetDialogFragment
import java.util.ArrayList

class AssetHandleSortStoreViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var parentCompanyId: Int = 0
    var type: String = "store"
    var storehouseId: Int? = 0
    var sortId: Int? = 0
    var manageMode: Int? = 0
    var currentPage = 1
    var pageSize = 10
    val uc = UiChangeEvent()
    var sortIdInfoList: HashMap<String, Int> = hashMapOf()

    var sStorehouseList: MutableList<StorehouseBean.StoreHouseListBean> = arrayListOf()
    var sSortList: MutableList<SortBean> = arrayListOf()
    var storehouseNameList: MutableList<String> = arrayListOf()
    var sortNameList: MutableList<String> = arrayListOf()
    var storehouseInfo = ObservableField("请选择仓库")
    var sortInfo = ObservableField("请选择物品")

    var sortList: MutableList<AssetHandleSortBean> = arrayListOf()
//    var productList: List<AssetHandleSortBean>? = arrayListOf()

    class UiChangeEvent {
        val loadSortEvent: SingleLiveEvent<MutableList<AssetHandleSortBean>> = SingleLiveEvent()
        val loadProductEvent: SingleLiveEvent<ListBean<AssetHandleSortBean>> = SingleLiveEvent()
        val loadSubmitEvent: SingleLiveEvent<MutableList<AssetHandleSortBean>> = SingleLiveEvent()
        val loadChangeEvent: SingleLiveEvent<Int> = SingleLiveEvent()
    }

    val selectedStore: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.loadChangeEvent.postValue(1)
    })
    val selectedSort: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.loadChangeEvent.postValue(2)
    })
    val reSearch: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.loadChangeEvent.postValue(0)
    })

    val selectedSubmit: BindingCommand<Void> = BindingCommand(BindingAction {
        var selectedSortList: MutableList<AssetHandleSortBean>? = arrayListOf()
        for (bean in sortList!!) {
            if (bean.manageMode == 1 || type != "store") {
                if (bean.selected != null && bean.selected!!) {
                    bean.handleSelectType = type
                    selectedSortList?.add(bean)
                }
            } else {
                if (bean.assetCountStr != null && !bean.assetCountStr!!.isEmpty()) {
                    bean.assetCount =  bean.assetCountStr!!.toInt()
                    if (bean.assetCount!! > bean.stockNum!!) {
                        showNormalToast("物品（" + bean.sortName + "）处置数量大于待处置数量")
                        return@BindingAction
                    }
                    bean.handleSelectType = type
                    selectedSortList?.add(bean)
                }
            }
        }
        uc.loadSubmitEvent.postValue(selectedSortList)
    })

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        this.getDataList()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 1
        this.getDataList()
    })
    fun getDataList () {
        if (storehouseId != null && storehouseId!! > 0 && sortId != null && sortId!! > 0) {
            if (type == "store") {
                if (manageMode == 1) {
                    getProcessProStockInfo()
                } else {
                    getProcessSortStockInfo()
                }
            } else if (type == "damage") {
                if (manageMode == 1) {
                    getProcessDamageProListStore()
                } else {
                    getProcessDamageSortList()
                }
            } else if (type == "inven") {
                if (manageMode == 1) {
                    getProcessInventoryProList()
                } else {
                    getProcessInventorySortList()
                }
            }
        }
    }
    fun getStorehouseList (
        companyId: String? = model.getLoginCompanyId(),
        userId: String? = model.getLocalUserInfo()?.userId
    ) {
        var keeperInfo: Int = 0
        model.mTengYunHttpDataSource.getOrgUseManageStorehouse(
            GsonUtils.toJson(
                mapOf(
                    "companyId" to companyId,
                    "userId" to userId,
                    "keeperInfo" to keeperInfo
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<StorehouseBean>>() {
                override fun onResult(t: BaseBean<StorehouseBean>) {
                    if (t.state == 0) {
                        sStorehouseList = t.data?.list as MutableList<StorehouseBean.StoreHouseListBean>
                        for(bean in sStorehouseList){
                            storehouseNameList.add(bean.storehouseName!!)
                        }
                    }
                    dismissLoading()
                }
                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
    fun getSortList (
        companyId: String? = model.getLoginCompanyId(),
        userId: String? = model.getLocalUserInfo()?.userId
    ) {
        var sortType: Int = 1
        model.mTengYunHttpDataSource.getSortAllList(
            GsonUtils.toJson(
                mapOf(
                    "companyId" to companyId,
                    "sortType" to sortType,
                    "propCompanyId" to parentCompanyId
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<SortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<SortBean>>) {
                    if (t.errorCode == 0) {
                        sSortList = t.data?.list as MutableList<SortBean>
                        for(bean in sSortList){
                            sortNameList.add(bean.sortName!!)
                        }
                    }
                    dismissLoading()
                }
                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }

    fun getProcessSortStockInfo (){
        val isAssets: Int ? = 0
        val applyId: Int? = null
        model.mTengYunHttpDataSource.getProcessSortStockInfo(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to pageSize,
                    "applyId" to applyId,
                    "isAssets" to isAssets,
                    "sortId" to sortId,
                    "storehouseId" to storehouseId,
                    "propCompanyId" to parentCompanyId
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<MutableList<AssetHandleSortBean>>>() {
                override fun onResult(t: BaseBean<
                        MutableList<AssetHandleSortBean>>) {
                    if (t.state == 0) {
                        uc.loadSortEvent.postValue(t.data)
                        sortList = t.data as MutableList<AssetHandleSortBean>
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }

    fun getProcessProStockInfo(){
        val isAssets: Int ? = 0
        val applyId: Int? = null
        model.mTengYunHttpDataSource.getProcessProStockInfo(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to pageSize,
                    "applyId" to applyId,
                    "isAssets" to isAssets,
                    "sortId" to sortId,
                    "storehouseId" to storehouseId,
                    "propCompanyId" to parentCompanyId
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<AssetHandleSortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<AssetHandleSortBean>>) {
                    if (t.errorCode == 0) {
                        if (currentPage == 1){
                            sortList.clear()
                        }
                        sortList.addAll(t.data?.list!!)
                        currentPage++
                        uc.loadProductEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
    fun getProcessDamageSortList (){
        val isAssets: Int ? = 0
        val applyId: Int? = null
        model.mTengYunHttpDataSource.getProcessDamageSortList(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to pageSize,
                    "applyId" to applyId,
                    "isAssets" to isAssets,
                    "sortId" to sortId,
                    "storehouseId" to storehouseId,
                    "propCompanyId" to parentCompanyId
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<AssetHandleSortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<AssetHandleSortBean>>) {
                    if (t.errorCode == 0) {
                        sortList = t.data?.list as MutableList<AssetHandleSortBean>
                        uc.loadSortEvent.postValue(sortList)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }

    fun getProcessDamageProListStore(
    ){
        val isAssets: Int ? = 0
        val applyId: Int? = null
        model.mTengYunHttpDataSource.getProcessDamageProListStore(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to pageSize,
                    "applyId" to applyId,
                    "isAssets" to isAssets,
                    "sortId" to sortId,
                    "storehouseId" to storehouseId,
                    "propCompanyId" to parentCompanyId
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<AssetHandleSortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<AssetHandleSortBean>>) {
                    if (t.errorCode == 0) {
                        if (currentPage == 1){
                            sortList.clear()
                        }
                        sortList.addAll(t.data?.list!!)
                        currentPage++
                        uc.loadProductEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }

    fun getProcessInventorySortList (){
        val isAssets: Int ? = 0
        val applyId: Int? = null
        model.mTengYunHttpDataSource.getProcessInventorySortList(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to pageSize,
                    "applyId" to applyId,
                    "isAssets" to isAssets,
                    "sortId" to sortId,
                    "storehouseId" to storehouseId,
                    "propCompanyId" to parentCompanyId
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<AssetHandleSortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<AssetHandleSortBean>>) {
                    if (t.errorCode == 0) {
                        sortList = t.data?.list as MutableList<AssetHandleSortBean>
                        uc.loadSortEvent.postValue(sortList)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }

    fun getProcessInventoryProList(
    ){
        val isAssets: Int ? = 0
        val applyId: Int? = null
        model.mTengYunHttpDataSource.getProcessInventoryProList(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to pageSize,
                    "applyId" to applyId,
                    "isAssets" to isAssets,
                    "sortId" to sortId,
                    "storehouseId" to storehouseId,
                    "propCompanyId" to parentCompanyId
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<AssetHandleSortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<AssetHandleSortBean>>) {
                    if (t.errorCode == 0) {
                        if (currentPage == 1){
                            sortList.clear()
                        }
                        sortList.addAll(t.data?.list!!)
                        currentPage++
                        uc.loadProductEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
}